package com.mahiru.mahiruaiagent.rag;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 编程学习助手向量数据库配置（初始化基于内存的向量数据库 Bean）
 *
 * @Author Enkidu
 * @Date 2025/7/5 16:49
 */
@Configuration
public class ChatAppVectorStoreConfig {
    // 注入loader

    @Resource
    private ChatAppDocumentLoader chatAppDocumentLoader;

    @Resource
    private MyKeywordEnricher myKeywordEnricher;

    // 初始化bean
    @Bean
    VectorStore chatAppVectorStore(EmbeddingModel embeddingModel) {
        // 定义VectorStore
        SimpleVectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
        List<Document> documentList = chatAppDocumentLoader.loadMarkdowns();

        // 增强文本
        List<Document> enrichedDocuments = myKeywordEnricher.enrichDocuments(documentList);
        // 装入文档
        vectorStore.add(documentList);
        // vectorStore.add(enrichedDocuments);
        return vectorStore;
    }
}
